
package com.bds.portal.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.aliyun.openservices.ons.api.SendResult;
import com.bds.model.Template;
import com.bds.model.TemplateDetail;
import com.bds.model.TemplateUrl;
import com.bds.model.Tresult;
import com.bds.portal.common.result.Page;
import com.bds.portal.queue.TcpUrlProducer;
import com.bds.portal.util.Const;

@Component
public class MyTUrlService {

	private static Logger logger = Logger.getLogger(MyTUrlService.class);

	@Resource
	private NutDao portalDao;

	public Page getTUrl(Long user_id, String request_id, Integer pageNum, Integer pageSize, Integer status) {
		Pager pager = portalDao.createPager(pageNum, pageSize);
		Criteria cri = Cnd.cri();

		cri.where().andEquals("user_id", user_id);
		cri.where().andEquals("request_id", request_id);
		if (status != null) {
			cri.where().andEquals("status", status);
		}
		List<TemplateUrl> list = portalDao.query(TemplateUrl.class, cri, pager);

		int count = portalDao.count(TemplateUrl.class, cri);
		Page p = new Page(count, pager.getPageNumber(), pager.getPageSize());
		p.setCode(Const.SUC);
		p.setData(list);
		return p;

	}

	public List<TemplateUrl> getAllTUrl(String request_id) {

		Criteria cri = Cnd.cri();

		cri.where().andEquals("request_id", request_id);
		List<TemplateUrl> list = portalDao.query(TemplateUrl.class, cri);

		return list;

	}

	public Template getTemplate(String request_id) {
		Template t = portalDao.fetch(Template.class, Cnd.where("request_id", "=", request_id));
		return t;

	}

	public boolean startUrl(Long user_id, String request_id, Integer status) {

		boolean ret = true;
		Criteria cri = Cnd.cri();

		cri.where().andEquals("user_id", user_id);
		cri.where().andEquals("request_id", request_id);
		if (status != null && status > 0) {
			cri.where().andEquals("status", status);
		}

		List<TemplateUrl> tt = portalDao.query(TemplateUrl.class, cri);

		for (TemplateUrl t : tt) {
//			JSONObject obj = new JSONObject();
//			obj.put("id", t.getId());
//			obj.put("requestId", t.getRequest_id());
//			obj.put("userId", t.getUser_id());
//			obj.put("table", "t_template_url");
//			obj.put("url", t.getUrl());
//			String msg = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss",
//					SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,
//					SerializerFeature.WriteDateUseDateFormat);
//			SendResult send = TcpUrlProducer.getInstance().send(msg.toString(), "url");
//			if (send != null && send.getMessageId() != null) {
				portalDao.update(TemplateUrl.class, Chain.make("send_status", 1), Cnd.where("id", "=", t.getId()));
				portalDao.clear(Tresult.class, Cnd.where("request_id", "=", request_id));
				List<TemplateDetail> all = portalDao.query(TemplateDetail.class,
						Cnd.where("request_id", "=", request_id).andNot("link", "is", null).andNot("link", "<>", ""));
				for (TemplateDetail detail : all) {
					portalDao.clear(Tresult.class, Cnd.where("request_id", "=", detail.getLink()));
				}
//			} else {
//				portalDao.update(TemplateUrl.class, Chain.make("send_status", 3), Cnd.where("id", "=", t.getId()));
//			}

		}

		return ret;

	}

	public boolean saveTUrl(Long user_id, String[] urls, String request_id) {

		boolean ret = true;

		for (int i = 0; i < urls.length; i++) {
			String url = urls[i];
			TemplateUrl t = new TemplateUrl();
			t.setCreate_time(new Date());
			t.setRequest_id(request_id);
			t.setStatus(0);
			t.setPage(0);
			t.setUrl(url);
			t.setOrderby((i + 1));
			t.setUser_id(user_id);
			t.setSend_status(0);
			try {
				portalDao.fastInsert(t);
			} catch (Exception e) {
				logger.error("", e);

			}
		}

		return ret;

	}

}
